<?php

namespace app\common\model;

use think\Model;

class Withdraw extends Model
{
    // 表名
    protected $name = 'cash_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'type_text',
        'time_text',
    ];
    

    public function user()
    {
        return $this->hasMany('User','id','user_id');
    }
    
    public function getTypeList()
    {
        $arr = [
            'Type_1'  =>  '充值',
            'Type_2'  =>  '交易收入',
            'Type_3'  =>  '交易支出',
            'Type_4'  =>  '提现'
        ];

        $list = [];
        for ($i=1;$i<=count($arr);$i++)
            $list[$i] = $arr['Type_'.$i];
        return $list;
    }     


    public function getTypeTextAttr($value,$data)
    {
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function getPayMannerAttr($value)
    {
        $pay = [
            0   =>  '余额',
            1   =>  '支付宝',
            2   =>  '微信',
            3   =>  '银行卡',
        ];
        return isset($value) ? $pay[$value] : $value;
    }
}
